﻿function RefreshTable(oTable, urlData) {
    //Retrieve the new data with $.getJSON. You could use it ajax too
    jQuery.getJSON(
            urlData,
            null,
            function (json) {
                table = oTable;
                oSettings = table.fnSettings();

                table.fnClearTable(this);

                for (var i = 0; i < json.aaData.length; i++) {
                    table.oApi._fnAddData(oSettings, json.aaData[i]);
                }

                oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();

                //table.fnClearTable(this);
                table.fnDraw();
            });
}
$(document).ready(function () {
    $("#wizard").smartWizard();
    $("#mws-tabs-preview").tabs({
        show: function (e, ui) {
            autoResize(".stepContainer", ".content");
        }
    });
    $("#mws-tabs-mr").tabs({
        show: function (e, ui) {
            autoResize(".stepContainer", ".content");
        }
    });

    var oTableAllergies = $('#tablelist-allergies').dataTable({
        "bSort": false,
        "sPaginationType": "full_numbers",
        "bProcessing": true,
        "bAutoWidth": false,
        "bFilter": false,
        "aoColumns": [
                        { "mDataProp": "Id", sClass: "row-align", sWidth: "20px" },
                        { "mDataProp": "AlergiesTo", sWidth: "80px" },
                        { "mDataProp": "Brand", sWidth: "120px" },
                        { "mDataProp": "IsGeneric", sWidth: "120px" },
                        { "mDataProp": "DescValue" },
                        { "mDataProp": "IsSubjective", sClass: "row-align", sWidth: "20px" }
                    ],
        "aoColumnDefs": [
                                {
                                    "fnRender": function (o) {
                                        return "<a class='view-allergies' href='javascript:void(0)' id='" + o.aData['Id'] + "' ><img src='/images/Icons/pencil.png'/></a>";
                                    },
                                    "aTargets": [0]
                                },
                                {
                                    "fnRender": function (o) {

                                        return o.aData["isSubjective"] == "0" ? "Yes" : "No";
                                    },
                                    "aTargets": [5]
                                },
                        ],

        "sAjaxSource": "/patientdata/medicalrecord/tablelistallergies"
    });

    var oTableMedicalHistories = $('#tablelist-medical-histories').dataTable({
        "bSort": false,
        "sPaginationType": "full_numbers",
        "bProcessing": true,
        "bAutoWidth": false,
        "bFilter": false,
        "aoColumns": [
                        { "mDataProp": "Id", sClass: "row-align", sWidth: "20px" },
                        { "mDataProp": "DiseaseHistory" },
                        { "mDataProp": "Year", sWidth: "50px" }

                    ],
        "aoColumnDefs": [
                                {
                                    "fnRender": function (o) {
                                        return "<a class='view-medical-histories' href='javascript:void(0)' id='" + o.aData['Id'] + "' ><img src='/images/Icons/pencil.png'/></a>";
                                    },
                                    "aTargets": [0]
                                }
                        ],
        "sAjaxSource": "/patientdata/medicalrecord/tablelistmedicalhistory"
    });


    var oTableFamilyHistories = $("#tablelist-family-histories").dataTable({
        "bSort": false,
        "sPaginationType": "full_numbers",
        "bProcessing": true,
        "bAutoWidth": false,
        "bFilter": false,
        "aoColumns": [
                        { "mDataProp": "Id", sClass: "row-align", sWidth: "20px" },
                        { "mDataProp": "DiseaseHistory" },
                        { "mDataProp": "Relations" }
                    ],
        "aoColumnDefs": [
                                {
                                    "fnRender": function (o) {
                                        return "<a class='view-family-histories' href='javascript:void(0)' id='" + o.aData['Id'] + "' ><img src='/images/Icons/pencil.png'/></a>";
                                    },
                                    "aTargets": [0]
                                }
                        ],
        "sAjaxSource": "/patientdata/medicalrecord/tablelistfamilyhistory"
    });

    var oTableHabit = $("#tablelist-habits").dataTable({
        "bSort": false,
        "sPaginationType": "full_numbers",
        "bProcessing": true,
        "bAutoWidth": false,
        "bFilter": false,
        "aoColumns": [
                        { "mDataProp": "Id", sClass: "row-align", sWidth: "20px" },
                        { "mDataProp": "Description" },
                        { "mDataProp": "Quantity" },
                        { "mDataProp": "Duration" }
                    ],
        "aoColumnDefs": [
                                {
                                    "fnRender": function (o) {
                                        return "<a class='view-habits' href='javascript:void(0)' id='" + o.aData['Id'] + "' ><img src='/images/Icons/pencil.png'/></a>";
                                    },
                                    "aTargets": [0]
                                }
                        ],
        "sAjaxSource": "/patientdata/medicalrecord/tablelisthabits"
    });

    var oTableOccupation = $("#tablelist-occupation").dataTable({
        "bSort": false,
        "sPaginationType": "full_numbers",
        "bProcessing": true,
        "bAutoWidth": false,
        "bFilter": false,
        "aoColumns": [
                        { "mDataProp": "Id", sClass: "row-align", sWidth: "20px" },
                        { "mDataProp": "Occupation" }
                    ],
        "aoColumnDefs": [
                                {
                                    "fnRender": function (o) {
                                        return "<a class='view-occupation' href='javascript:void(0)' id='" + o.aData['Id'] + "' ><img src='/images/Icons/pencil.png'/></a>";
                                    },
                                    "aTargets": [0]
                                }
                        ],
        "sAjaxSource": "/patientdata/medicalrecord/tablelistoccupation"
    });

    var oTableProblemListView = $("#tablelist-problem-list-view").dataTable({
        "bSort": false,
        "sPaginationType": "full_numbers",
        "bProcessing": true,
        "bAutoWidth": false,
        "bFilter": false,
        "aoColumns": [
                        { "mDataProp": "Id", sClass: "row-align", sWidth: "20px" },
                        { "mDataProp": "ProblemRecords" }
                    ],
        "aoColumnDefs": [
                                {
                                    "fnRender": function (o) {
                                        return "<a class='view-problem-list' href='javascript:void(0)' id='" + o.aData['Id'] + "' ><img src='/images/Icons/pencil.png'/></a>";
                                    },
                                    "aTargets": [0]
                                }
                        ],
        "sAjaxSource": "/patientdata/medicalrecord/tablelistproblemlist"
    });

    autoResize(".stepContainer", ".content");

    //Submit Form Ajax
    $("#allergies-view").submit(function () {
        var post_data = $(this).serialize();
        var form_action = $(this).attr("action");
        var form_method = $(this).attr("method");
        jQuery.ajax({
            type: form_method,
            url: form_action,
            cache: false,
            dataType: "json",
            data: post_data,
            success: function (data) {
                window.scrollTo(0, 0);
                return false;
            },
            error: function () {
            },
            complete: function () {
                RefreshTable(oTableAllergies, "/patientdata/medicalrecord/tablelistallergies");
            }
        });
        return false;
    });

    $("#medical-histories-view").submit(function () {
        var post_data = $(this).serialize();
        var form_action = $(this).attr("action");
        var form_method = $(this).attr("method");
        jQuery.ajax({
            type: form_method,
            url: form_action,
            cache: false,
            dataType: "json",
            data: post_data,
            success: function (data) {
                window.scrollTo(0, 0);
                return false;
            },
            error: function () {
            },
            complete: function () {
                RefreshTable(oTableMedicalHistories, "/patientdata/medicalrecord/tablelistmedicalhistory");
            }
        });
        return false;
    });

    $("#family-histories-view").submit(function () {
        var post_data = $(this).serialize();
        var form_action = $(this).attr("action");
        var form_method = $(this).attr("method");
        jQuery.ajax({
            type: form_method,
            url: form_action,
            cache: false,
            dataType: "json",
            data: post_data,
            success: function (data) {
                window.scrollTo(0, 0);
                return false;
            },
            error: function () {
            },
            complete: function () {
                RefreshTable(oTableFamilyHistories, "/patientdata/medicalrecord/tablelistfamilyhistory");
            }
        });
        return false;
    });
    $("#habits-view").submit(function () {
        var post_data = $(this).serialize();
        var form_action = $(this).attr("action");
        var form_method = $(this).attr("method");
        jQuery.ajax({
            type: form_method,
            url: form_action,
            cache: false,
            dataType: "json",
            data: post_data,
            success: function (data) {
                window.scrollTo(0, 0);
                return false;
            },
            error: function () {
            },
            complete: function () {
                RefreshTable(oTableHabit, "/patientdata/medicalrecord/tablelisthabits");
            }
        });
        return false;
    });
});
